package com.neuedu.his.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.his.po.Disecategory;          // ← 你的 PO 实体
import com.neuedu.his.service.DisecategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 诊断目录管理接口
 */
@RestController
@RequestMapping("/api/master/diag")
public class DiagController {

    @Autowired
    private DisecategoryService diagService;

    // ============================== 分页查询 ==============================

    /**
     * GET /api/master/diag?keyword=&page=1&pageSize=10
     */
    @GetMapping
    public IPage<Disecategory> page(
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "10") long pageSize,
            @RequestParam(required = false) String keyword) {

        LambdaQueryWrapper<Disecategory> qw = new LambdaQueryWrapper<>();
        qw.eq(Disecategory::getDelmark, 1);      // 1 = 有效

        if (StringUtils.isNotBlank(keyword)) {
            qw.and(w -> w.like(Disecategory::getDicacode, keyword)
                    .or().like(Disecategory::getDicaname, keyword));
        }
        qw.orderByAsc(Disecategory::getSequenceno);

        return diagService.page(new Page<>(page, pageSize), qw);
    }

    // ============================== 新增 / 修改 ==============================

    /**
     * POST /api/master/diag       （id 为空 = 新增）
     * PUT  /api/master/diag/{id}  （id 不为空 = 修改）
     */
    @PostMapping
    public boolean create(@RequestBody Disecategory dto) {
        dto.setDelmark(1);                    // 1 = 有效
        return diagService.save(dto);
    }

    @PutMapping("/{id}")
    public boolean update(@PathVariable Integer id, @RequestBody Disecategory dto) {
        dto.setId(id);
        return diagService.updateById(dto);
    }

    // ============================== 逻辑删除 ==============================

    /**
     * DELETE /api/master/diag/{id}
     */
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Integer id) {
        Disecategory tmp = new Disecategory();
        tmp.setId(id);
        tmp.setDelmark(0);                    // 0 = 已删除
        return diagService.updateById(tmp);
    }
}
